package com.tech.koufu.ui.adapter;

import java.util.ArrayList;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.tech.koufu.R;
import com.tech.koufu.model.CompetitionGroup;
import com.tech.koufu.tools.Statics;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter; 
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 比赛列表
 * @author 123
 *
 */
public class CompetitionAdapter extends BaseAdapter {
	private Context context;  
	private LayoutInflater mInflater = null; 
	public ArrayList<CompetitionGroup> datas;
	BitmapUtils bitmapUtils;
	public CompetitionAdapter(Context context, BitmapUtils bitmapUtils){
		this.context = context;
		this.mInflater = LayoutInflater.from(context); 
		this.bitmapUtils = bitmapUtils;
	}

	@Override
	public int getCount() {   
		return datas.size();
	}

	@Override
	public Object getItem(int position) { 
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_competition, parent,false); 
             
             holder.icon = (ImageView)convertView.findViewById(R.id.iv_competition);
             holder.txt_compe_name = (TextView)convertView.findViewById(R.id.txt_compe_name);
             holder.txt_compe_participates = (TextView)convertView.findViewById(R.id.txt_compe_participates);
             holder.txt_compe_time = (TextView)convertView.findViewById(R.id.txt_compe_time);
             holder.txt_compe_founder = (TextView)convertView.findViewById(R.id.txt_compe_founder);
             holder.txt_compe_intros = (TextView)convertView.findViewById(R.id.txt_compe_intros);
             
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         CompetitionGroup cg = (CompetitionGroup) getItem(position);
         holder.txt_compe_name.setText(cg.name);
         
         SpannableStringBuilder style=new SpannableStringBuilder("人数："+cg.memberQuantity);          
         style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.bgBlackColor_63)),0,3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
         holder.txt_compe_participates.setText(style);
         
         holder.txt_compe_time.setText(cg.addTime); 
         holder.txt_compe_founder .setText(String.format(context.getString(R.string.competition_founder_tt), cg.userName));
         if(cg.cover!=null){  
         	try {
				bitmapUtils.display(holder.icon , Statics.URL_IMG_CONTECT + cg.cover);
			} catch (Exception e) { 
				e.printStackTrace();
			}
         }
		return convertView;
	}
	static class ViewHolder  
    {   
		public ImageView icon;
        public TextView txt_compe_name;  
        public TextView txt_compe_participates;  
        public TextView txt_compe_time;  
        public TextView txt_compe_founder;  
        public TextView txt_compe_intros;  
        
    }  
}   
